package com.lagou.rabbitmq.demo.listener;

import com.lagou.rabbitmq.demo.dao.OrderDao;
import com.lagou.rabbitmq.demo.pojo.Order;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

@Component
public class MyMeetingListener {
    @Autowired
    private OrderDao orderDao;

    @RabbitListener(queues = "queue.delayed")
    public void onMessage(Message message, Channel channel) throws IOException {

        System.out.println("==="+new String(message.getBody(), message.getMessageProperties().getContentEncoding()));


        // 消息确认
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

        List<Order> orders = orderDao.queryByOrder_no(new String(message.getBody()));
        if(orders!=null && orders.size()>0) {
            if ("0".equals(orders.get(0).getO_type())) {
                System.out.println(new String(message.getBody())+"超时未支付");
                orderDao.updateStatusById(new String(message.getBody()));
            }else if("1".equals(orders.get(0).getO_type())){
                System.out.println(new String(message.getBody())+"支付成功");
            }
        }

    }


}
